<template>
  <div><van-nav-bar
  title="账号登录"
  left-arrow
  @click-left="onClickLeft"
/>
<van-form @submit="onSubmit">
  <div class="main"> <van-field
    v-model="formLogin.username"
    placeholder="请输入账号"
  />
  <van-field
    v-model="formLogin.password"
    type="password"
    placeholder="请输入密码"
  />
  <div style="margin: 16px;"></div>
    <van-button round block type="info" native-type="submit" :disabled='isloding'  square>提交</van-button>
        <a class="zhuce">还没有账号，去注册</a>
  </div>

</van-form>
</div>
</template>
<script>
import { Toast } from 'vant'
import { mapActions } from 'vuex'
export default {

  data () {
    return {
      formLogin: {
        username: 'hzhmqd',
        password: '123456'
      },
      isloding: false
    }
  },
  methods: {
    ...mapActions(['asyncUserLogs']),
    async  onSubmit () {
      if (this.formLogin.username === '' || this.formLogin.password === '') {
        Toast({
          message: '账号和用户名不得为空'
        })
        return
      }
      if (!RegExp(/^[a-zA-z0-9]{5,8}$/).test(this.formLogin.username)) {
        Toast({
          message: '用户名为5-8位'
        })
        return
      }
      if (!RegExp(/^[a-zA-z0-9]{5,12}$/).test(this.formLogin.password)) {
        Toast({
          message: '密码为5-12位'
        })
        return
      }
      Toast.loading({
        forbidClick: this.isloding,
        message: '加载中...'
      })
      this.isloding = true
      try {
        this.isloding = false
        await this.asyncUserLogs(this.formLogin)
        Toast.success('登录成功')
        await this.$router.push({
          path: '/layout/mine'
        })
      } catch (err) {
        console.log(err)
        this.isloding = false
        Toast({
          message: '用户名或密码错误'
        })
      }
    },
    onClickLeft () {
      this.$router.push({
        path: '/layout/mine'
      })
    }
  }
}
</script>

<style lang='less' scoped>
.main{
  margin: 20px 0 20px 0 ;
  .zhuce{
    font-size: 14px;
    color: #666;

  }
}
</style>
<style lang='less' scoped>

</style>
